package com.gsta.cdn.cdr.controller;

import com.gsta.cdn.cdr.entity.ResultEntity;
import com.gsta.cdn.cdr.mapper.ConcurrentMapper;
import com.gsta.cdn.cdr.service.ConcurrentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(path = "/Concurrent")
public class ConcurrentController {
    @Autowired
    private ConcurrentMapper concurrentMapper;

    @Resource
    private ConcurrentService concurrentService;

    //并发统计
    @RequestMapping(path = "/ChannelConcurrent", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ChannelConcurrent(@RequestParam(required = true) String date,
                                             HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentService.ChannelConcurrent(date);
//        List<Map<String, Object>> vodlist = concurrentMapper.VodConcurrent(date);
//        list.addAll(vodlist);
        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
//            resultEntity.setData(vodlist);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //并发地区列表
    @RequestMapping(path = "/ConcurrentArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentArea(@RequestParam(required = true) String date,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentService.ConcurrentArea(date);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //并发地区PopName列表
    @RequestMapping(path = "/ConcurrentAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentAreaPopName(@RequestParam(required = true) String date,
                                              @RequestParam(required = true) String area,
                                       HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentService.ConcurrentAreaPopName(date,area);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //并发地区统计
    @RequestMapping(path = "/ConcurrentByArea", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentByArea(@RequestParam(required = true) String date,
                                         @RequestParam(required = true) String area,
                                       HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentService.ConcurrentByArea(date,area);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //并发地区PopName统计
    @RequestMapping(path = "/ConcurrentByAreaPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentByAreaPopName(@RequestParam(required = true) String date,
                                         @RequestParam(required = true) String area,
                                                @RequestParam(required = true) String popName,
                                         HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentService.ConcurrentByAreaPopName(date,area,popName);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }


    //并发PopID统计
    @RequestMapping(path = "/ConcurrentByPopID", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentByPopID(@RequestParam(required = true) String date,
                                         @RequestParam(required = true) String popID,
                                         HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentService.ConcurrentByPopID(date,popID);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }


    //并发PopName统计
    @RequestMapping(path = "/ConcurrentByPopName", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentByPopName(@RequestParam(required = true) String date,
                                          @RequestParam(required = true) String popName,
                                          HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentService.ConcurrentByPopName(date,popName);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }

    //并发PopID列表
    @RequestMapping(path = "/ConcurrentPopID", method = RequestMethod.POST)
    @ResponseBody
    public ResultEntity ConcurrentPopID(@RequestParam(required = true) String date,
                                       HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Cache-Control","no-cache");
        List<Map<String, Object>> list = concurrentService.ConcurrentPopID(date);

        ResultEntity resultEntity = new ResultEntity();
        try {
            resultEntity.setData(list);
            if (list.size() > 0){
                resultEntity.setMsg("查询成功");
                resultEntity.setCode(0);
            }else{
                resultEntity.setMsg("无数据");
                resultEntity.setCode(-1);
            }
            return resultEntity;
        }
        catch (Exception e){
            return ResultEntity.error("查询失败");
        }
    }
}
